﻿using ControllerManageServer.ActionModel;
using ControllerManageServer.ActionModel.Aioi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ControllerManageServer.LabelDevice.Aioi
{
    class MWU2060LabelDevice : BaseLabelDevice
    {

        public MWU2060LabelDevice()
        {
            SeqLength = 6;
        }

        /// <summary>
        /// 手环ID
        /// </summary>
        private string UhfRfId { get; set; }
        /// <summary>
        /// 手环ID掩码
        /// </summary>
        private string EpcMask { get; set; }

        /// <summary>
        /// MWU2060标签的动作指令(手环ID为空时,使用SC002命令,手环ID不为空时,使用SC001命令)
        /// </summary>
        /// <param name="iModel">模式组合</param>
        /// <param name="iSeq">显示数据</param>
        /// <param name="iFnSeq">按下Fn后的显示数据</param>
        /// <returns></returns>
        public override byte[] Action(ActionModelAbstract iModel, string iSeq, string iFnSeq = null)
        {
            if (iModel is AioiActionModelMWU2060)
            {
                UhfRfId = ((AioiActionModelMWU2060)iModel).UhfRfId;
                EpcMask = ((AioiActionModelMWU2060)iModel).EpcMask;
            }
            if (string.IsNullOrEmpty(UhfRfId))
            {
                return ActionSC002(iModel, iSeq, iFnSeq);
            }
            return ActionSC001(iModel, iSeq, iFnSeq);
        }

        /// <summary>
        /// 根据手环ID进行熄灯
        /// </summary>
        /// <param name="iModel"></param>
        /// <param name="iSeq"></param>
        /// <param name="iFnSeq"></param>
        /// <returns></returns>
        public byte[] ActionSC001(ActionModelAbstract iModel, string iSeq, string iFnSeq = null)
        {
            if (UhfRfId.Length != 24)
            {
                throw new Exception($"UHF_ID error[{UhfRfId}]; The length of the UHF_ID must be 24!");
            }
            if (!(iModel is AioiActionModelMWU2060))
            {
                throw new Exception($"MWU2060 Mode Array Exception!");
            }

            List<byte> result = new List<byte>();

            result.AddRange(DefaultEncoding.GetBytes("SC001"));
            result.Add((byte)(((AioiActionModelAbstract)iModel).ConfirmKey.BinValue.Equals(AioiActionModelAbstract.Func.NOT_USED.BinValue) ? '2' : '1'));// 1:作业,2:显示,5:可重复的作业
            result.AddRange(DefaultEncoding.GetBytes(string.IsNullOrEmpty(iFnSeq) ? "00" : "06"));// 指定按Fn后是否显示数据
            result.Add((byte)(string.IsNullOrEmpty(EpcMask) ? '0' : '1'));// 0:没有EPC Mask数据，1:有EPC Mask数据
            result.AddRange(DefaultEncoding.GetBytes("00"));// 区域号码
            result.AddRange(iModel.ModeArray());// 模式组合
            result.AddRange(DefaultEncoding.GetBytes(Address));// 地址
            if (iSeq == null) iSeq = "";
            result.AddRange(DefaultEncoding.GetBytes(SeqHandle(iSeq)));// 显示的数据
            if (!string.IsNullOrEmpty(iFnSeq))
            {
                result.AddRange(DefaultEncoding.GetBytes(SeqHandle(iFnSeq)));// Fn键按下后显示数据
            }
            result.AddRange(DefaultEncoding.GetBytes(UhfRfId));// RFID
            if (!string.IsNullOrEmpty(EpcMask))
            {
                result.AddRange(DefaultEncoding.GetBytes(EpcMask.PadLeft(24, ' ').Substring(0, 24)));// 手环ID掩码
            }

            return result.ToArray();
        }

        /// <summary>
        /// 通过手环进行熄灯
        /// </summary>
        /// <param name="iModel"></param>
        /// <param name="iSeq"></param>
        /// <param name="iFnSeq"></param>
        /// <returns></returns>
        public byte[] ActionSC002(ActionModelAbstract iModel, string iSeq, string iFnSeq = null)
        {
            if (!(iModel is AioiActionModelMWU2060))
            {
                throw new Exception($"MWU2060 Mode Array Exception!");
            }

            List<byte> result = new List<byte>();

            result.AddRange(DefaultEncoding.GetBytes("SC002"));
            result.Add((byte)(((AioiActionModelAbstract)iModel).ConfirmKey.BinValue.Equals(AioiActionModelAbstract.Func.NOT_USED.BinValue) ? '2' : '1'));// 1:作业,2:显示,5:可重复的作业
            result.AddRange(DefaultEncoding.GetBytes(string.IsNullOrEmpty(iFnSeq) ? "00" : "06"));// 指定按Fn后是否显示数据
            result.AddRange(DefaultEncoding.GetBytes("00"));// 区域号码
            result.AddRange(iModel.ModeArray());// 模式组合
            result.AddRange(DefaultEncoding.GetBytes(Address));// 地址
            if (iSeq == null) iSeq = "";
            result.AddRange(DefaultEncoding.GetBytes(SeqHandle(iSeq)));// 显示的数据
            if (!string.IsNullOrEmpty(iFnSeq))
            {
                result.AddRange(DefaultEncoding.GetBytes(SeqHandle(iFnSeq)));// Fn键按下后显示数据
            }

            return result.ToArray();
        }

    }
}